<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    kubeadm高可用master节点三主两从.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-k8s/kubeadm高可用master节点三主两从"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  kubeadm高可用master节点三主两从.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/k8s/kubeadm%E9%AB%98%E5%8F%AF%E7%94%A8master%E8%8A%82%E7%82%B9%E4%B8%89%E4%B8%BB%E4%B8%A4%E4%BB%8E/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/k8s/">k8s</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">7k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">34 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="kubeadm高可用master节点（三主两从）"><a href="#kubeadm高可用master节点（三主两从）" class="headerlink" title="kubeadm高可用master节点（三主两从）"></a>kubeadm高可用master节点（三主两从）</h1><h2 id="1、安装要求"><a href="#1、安装要求" class="headerlink" title="1、安装要求"></a>1、安装要求</h2><p>在开始之前，部署Kubernetes集群机器需要满足以下几个条件：</p>
<ul>
<li>五台机器，操作系统 CentOS7.5+（mini）</li>
<li>硬件配置：2GBRAM，2vCPU+，硬盘30GB+</li>
<li>集群中所有机器之间网络互通，且可访问外网。</li>
</ul>
<h2 id="2、安装步骤"><a href="#2、安装步骤" class="headerlink" title="2、安装步骤"></a>2、安装步骤</h2><table>
<thead>
<tr>
<th align="left">角色</th>
<th align="left">IP</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>k8s-lb</code></td>
<td align="left">192.168.50.100</td>
</tr>
<tr>
<td align="left"><code>master1</code></td>
<td align="left">192.168.50.128</td>
</tr>
<tr>
<td align="left"><code>master2</code></td>
<td align="left">192.168.50.129</td>
</tr>
<tr>
<td align="left"><code>master3</code></td>
<td align="left">192.168.50.130</td>
</tr>
<tr>
<td align="left"><code>node1</code></td>
<td align="left">192.168.50.131</td>
</tr>
<tr>
<td align="left"><code>node2</code></td>
<td align="left">192.168.50.132</td>
</tr>
</tbody></table>
<h3 id="2-1、安装前预处理操作"><a href="#2-1、安装前预处理操作" class="headerlink" title="2.1、安装前预处理操作"></a>2.1、安装前预处理操作</h3><h4 id="（1）配置主机名"><a href="#（1）配置主机名" class="headerlink" title="（1）配置主机名"></a>（1）配置主机名</h4><p><code>master1</code>节点设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# hostnamectl set-hostname master1</span><br></pre></td></tr></table></figure>

<p><code>master2</code>节点设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# hostnamectl set-hostname master2</span><br></pre></td></tr></table></figure>

<p><code>master3</code>节点设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# hostnamectl set-hostname master3</span><br></pre></td></tr></table></figure>

<p><code>node1</code>从节点设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# hostnamectl set-hostname node1</span><br></pre></td></tr></table></figure>

<p><code>node2</code>从节点设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# hostnamectl set-hostname node2</span><br></pre></td></tr></table></figure>

<p><strong>执行<code>bash</code>命令以加载新设置的主机名</strong></p>
<h4 id="（2）添加hosts"><a href="#（2）添加hosts" class="headerlink" title="（2）添加hosts"></a>（2）添加<code>hosts</code></h4><p>所有的节点都要添加<code>hosts</code>解析记录</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">~]# cat &gt;&gt;&#x2F;etc&#x2F;hosts &lt;&lt;EOF</span><br><span class="line">192.168.50.100 k8s-lb</span><br><span class="line">192.168.50.128 master1</span><br><span class="line">192.168.50.129 master2</span><br><span class="line">192.168.50.130 master3</span><br><span class="line">192.168.50.131 node1</span><br><span class="line">192.168.50.132 node2</span><br><span class="line">EOF</span><br></pre></td></tr></table></figure>

<h4 id="（3）配置免密"><a href="#（3）配置免密" class="headerlink" title="（3）配置免密"></a>（3）配置免密</h4><p>在<code>master1</code>节点生成密钥对，并分发给其他的所有主机。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# ssh-keygen -t rsa -b 1200</span><br><span class="line">Generating public&#x2F;private rsa key pair.</span><br><span class="line">Enter file in which to save the key (&#x2F;root&#x2F;.ssh&#x2F;id_rsa): </span><br><span class="line">Enter passphrase (empty for no passphrase): </span><br><span class="line">Enter same passphrase again: </span><br><span class="line">Your identification has been saved in &#x2F;root&#x2F;.ssh&#x2F;id_rsa.</span><br><span class="line">Your public key has been saved in &#x2F;root&#x2F;.ssh&#x2F;id_rsa.pub.</span><br><span class="line">The key fingerprint is:</span><br><span class="line">SHA256:OoMw1dARsWhbJKAQL2hUxwnM4tLQJeLynAQHzqNQs5s root@localhost.localdomain</span><br><span class="line">The key&#39;s randomart image is:</span><br><span class="line">+---[RSA 1200]----+</span><br><span class="line">|*&#x3D;X&#x3D;*o*+         |</span><br><span class="line">|OO.*.O..         |</span><br><span class="line">|BO&#x3D; + +          |</span><br><span class="line">|**o* o           |</span><br><span class="line">|o E .   S        |</span><br><span class="line">|   o . .         |</span><br><span class="line">|    . +          |</span><br><span class="line">|       o         |</span><br><span class="line">|                 |</span><br><span class="line">+----[SHA256]-----+</span><br></pre></td></tr></table></figure>

<h5 id="分发公钥"><a href="#分发公钥" class="headerlink" title="分发公钥"></a>分发公钥</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# ssh-copy-id -i ~&#x2F;.ssh&#x2F;id_rsa.pub  root@master1</span><br><span class="line">[root@master1 ~]# ssh-copy-id -i ~&#x2F;.ssh&#x2F;id_rsa.pub  root@master2</span><br><span class="line">[root@master1 ~]# ssh-copy-id -i ~&#x2F;.ssh&#x2F;id_rsa.pub  root@master3</span><br><span class="line">[root@master1 ~]# ssh-copy-id -i ~&#x2F;.ssh&#x2F;id_rsa.pub  root@node1</span><br><span class="line">[root@master1 ~]# ssh-copy-id -i ~&#x2F;.ssh&#x2F;id_rsa.pub  root@node2</span><br></pre></td></tr></table></figure>

<h4 id="（4）升级内核"><a href="#（4）升级内核" class="headerlink" title="（4）升级内核"></a>（4）升级内核</h4><p>通过下载<code>kernel image</code>的rpm包进行安装。</p>
<p>centos7系统：<code>http://elrepo.org/linux/kernel/el7/x86_64/RPMS/</code></p>
<p><img src="http://iubest.gitee.io/pic/640-1601003120729.webp" alt="img"></p>
<p>编写<code>shell</code>脚本升级内核</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#!&#x2F;bin&#x2F;bash</span><br><span class="line"># ----------------------------</span><br><span class="line"># upgrade kernel by bomingit@126.com</span><br><span class="line"># ----------------------------</span><br><span class="line"></span><br><span class="line">yum localinstall -y kernel-lt*</span><br><span class="line">if [ $? -eq 0 ];then</span><br><span class="line"> grub2-set-default 0 &amp;&amp; grub2-mkconfig -o &#x2F;etc&#x2F;grub2.cfg</span><br><span class="line"> grubby --args&#x3D;&quot;user_namespace.enable&#x3D;1&quot; --update-kernel&#x3D;&quot;$(grubby --default-kernel)&quot;</span><br><span class="line">fi</span><br><span class="line">echo &quot;please reboot your system quick!!!&quot;</span><br></pre></td></tr></table></figure>

<p>注意：<strong>一定要重启机器</strong></p>
<h5 id="验证内核版本"><a href="#验证内核版本" class="headerlink" title="验证内核版本"></a>验证内核版本</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# uname -r</span><br><span class="line">4.4.229-1.el7.elrepo.x86_64</span><br></pre></td></tr></table></figure>

<h4 id="（5）关闭防火墙selinux"><a href="#（5）关闭防火墙selinux" class="headerlink" title="（5）关闭防火墙selinux"></a>（5）关闭防火墙<code>selinux</code></h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">~]# systemctl disable --now firewalld</span><br><span class="line">~]# setenforce 0</span><br><span class="line">~]# sed -i &#39;s&#x2F;enforcing&#x2F;disabled&#x2F;&#39; &#x2F;etc&#x2F;selinux&#x2F;config </span><br></pre></td></tr></table></figure>

<p>上面的是临时关闭，当然也可以永久关闭，即在<code>/etc/fstab</code>文件中将<code>swap</code>挂载所在的行注释掉即可。</p>
<h4 id="（6）关闭swap分区"><a href="#（6）关闭swap分区" class="headerlink" title="（6）关闭swap分区"></a>（6）关闭<code>swap</code>分区</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">~]# swapoff -a</span><br><span class="line">~]# sed -i.bak &#39;s&#x2F;^.*centos-swap&#x2F;#&amp;&#x2F;g&#39; &#x2F;etc&#x2F;fstab</span><br></pre></td></tr></table></figure>

<p>第一条是临时关闭，当然也可以使用第二条永久关闭，后者手动在<code>/etc/fstab</code>文件中将<code>swap</code>挂载所在的行注释掉即可。</p>
<h4 id="（7）优化内核"><a href="#（7）优化内核" class="headerlink" title="（7）优化内核"></a>（7）优化内核</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">~]# cat &gt; &#x2F;etc&#x2F;sysctl.d&#x2F;k8s.conf &lt;&lt; EOF</span><br><span class="line">net.ipv4.ip_forward &#x3D; 1</span><br><span class="line">net.bridge.bridge-nf-call-iptables &#x3D; 1</span><br><span class="line">net.bridge.bridge-nf-call-ip6tables &#x3D; 1</span><br><span class="line">fs.may_detach_mounts &#x3D; 1</span><br><span class="line">vm.overcommit_memory&#x3D;1</span><br><span class="line">vm.panic_on_oom&#x3D;0</span><br><span class="line">fs.inotify.max_user_watches&#x3D;89100</span><br><span class="line">fs.file-max&#x3D;52706963</span><br><span class="line">fs.nr_open&#x3D;52706963</span><br><span class="line">net.ipv4.tcp_keepalive_time &#x3D; 600</span><br><span class="line">net.ipv4.tcp.keepaliv.probes &#x3D; 3</span><br><span class="line">net.ipv4.tcp_keepalive_intvl &#x3D; 15</span><br><span class="line">net.ipv4.tcp.max_tw_buckets &#x3D; 36000</span><br><span class="line">net.ipv4.tcp_tw_reuse &#x3D; 1</span><br><span class="line">net.ipv4.tcp.max_orphans &#x3D; 327680</span><br><span class="line">net.ipv4.tcp_orphan_retries &#x3D; 3</span><br><span class="line">net.ipv4.tcp_syncookies &#x3D; 1</span><br><span class="line">net.ipv4.tcp_max_syn_backlog &#x3D; 16384</span><br><span class="line">net.ipv4.ip_conntrack_max &#x3D; 65536</span><br><span class="line">net.ipv4.tcp_max_syn_backlog &#x3D; 16384</span><br><span class="line">net.ipv4.top_timestamps &#x3D; 0</span><br><span class="line">net.core.somaxconn &#x3D; 16384</span><br><span class="line">EOF</span><br></pre></td></tr></table></figure>

<p>使其立即生效</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# sysctl --system</span><br></pre></td></tr></table></figure>

<h4 id="（8）配置yum源"><a href="#（8）配置yum源" class="headerlink" title="（8）配置yum源"></a>（8）配置<code>yum</code>源</h4><p>所有的节点均采用阿里云官网的<code>base</code>和<code>epel</code>源</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">~]# mv &#x2F;etc&#x2F;yum.repos.d&#x2F;* &#x2F;tmp</span><br><span class="line">~]# curl -o &#x2F;etc&#x2F;yum.repos.d&#x2F;CentOS-Base.repo https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;repo&#x2F;Centos-7.repo</span><br><span class="line">~]# curl -o &#x2F;etc&#x2F;yum.repos.d&#x2F;epel.repo http:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;repo&#x2F;epel-7.repo</span><br></pre></td></tr></table></figure>

<h4 id="（9）时区与时间同步"><a href="#（9）时区与时间同步" class="headerlink" title="（9）时区与时间同步"></a>（9）时区与时间同步</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">~]# ln -sf &#x2F;usr&#x2F;share&#x2F;zoneinfo&#x2F;Asia&#x2F;Shanghai &#x2F;etc&#x2F;localtime</span><br><span class="line">~]# yum install dnf ntpdate -y</span><br><span class="line">~]# ntpdate ntp.aliyun.com</span><br></pre></td></tr></table></figure>

<h4 id="（10）编写shell"><a href="#（10）编写shell" class="headerlink" title="（10）编写shell"></a>（10）编写<code>shell</code></h4><p>将上面的第5-8步骤写成<code>shell</code>脚本自动化快速完成</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">#!&#x2F;bin&#x2F;sh</span><br><span class="line">#****************************************************************#</span><br><span class="line"># ScriptName: init.sh</span><br><span class="line"># Author: boming</span><br><span class="line"># Create Date: 2020-06-23 22:19</span><br><span class="line">#***************************************************************#</span><br><span class="line"></span><br><span class="line">#关闭防火墙</span><br><span class="line">systemctl disable --now firewalld</span><br><span class="line">setenforce 0</span><br><span class="line">sed -i &#39;s&#x2F;enforcing&#x2F;disabled&#x2F;&#39; &#x2F;etc&#x2F;selinux&#x2F;config</span><br><span class="line">#关闭swap分区</span><br><span class="line">swapoff -a</span><br><span class="line">sed -i.bak &#39;s&#x2F;^.*centos-swap&#x2F;#&amp;&#x2F;g&#39; &#x2F;etc&#x2F;fstab</span><br><span class="line">#优化系统</span><br><span class="line">cat &gt; &#x2F;etc&#x2F;sysctl.d&#x2F;k8s.conf &lt;&lt; EOF</span><br><span class="line">net.ipv4.ip_forward &#x3D; 1</span><br><span class="line">net.bridge.bridge-nf-call-iptables &#x3D; 1</span><br><span class="line">net.bridge.bridge-nf-call-ip6tables &#x3D; 1</span><br><span class="line">fs.may_detach_mounts &#x3D; 1</span><br><span class="line">vm.overcommit_memory&#x3D;1</span><br><span class="line">vm.panic_on_oom&#x3D;0</span><br><span class="line">fs.inotify.max_user_watches&#x3D;89100</span><br><span class="line">fs.file-max&#x3D;52706963</span><br><span class="line">fs.nr_open&#x3D;52706963</span><br><span class="line">net.ipv4.tcp_keepalive_time &#x3D; 600</span><br><span class="line">net.ipv4.tcp.keepaliv.probes &#x3D; 3</span><br><span class="line">net.ipv4.tcp_keepalive_intvl &#x3D; 15</span><br><span class="line">net.ipv4.tcp.max_tw_buckets &#x3D; 36000</span><br><span class="line">net.ipv4.tcp_tw_reuse &#x3D; 1</span><br><span class="line">net.ipv4.tcp.max_orphans &#x3D; 327680</span><br><span class="line">net.ipv4.tcp_orphan_retries &#x3D; 3</span><br><span class="line">net.ipv4.tcp_syncookies &#x3D; 1</span><br><span class="line">net.ipv4.tcp_max_syn_backlog &#x3D; 16384</span><br><span class="line">net.ipv4.ip_conntrack_max &#x3D; 65536</span><br><span class="line">net.ipv4.tcp_max_syn_backlog &#x3D; 16384</span><br><span class="line">net.ipv4.top_timestamps &#x3D; 0</span><br><span class="line">net.core.somaxconn &#x3D; 16384</span><br><span class="line">EOF</span><br><span class="line">#立即生效</span><br><span class="line">sysctl --system</span><br><span class="line">#配置阿里云的base和epel源</span><br><span class="line">mv &#x2F;etc&#x2F;yum.repos.d&#x2F;* &#x2F;tmp</span><br><span class="line">curl -o &#x2F;etc&#x2F;yum.repos.d&#x2F;CentOS-Base.repo https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;repo&#x2F;Centos-7.repo</span><br><span class="line">curl -o &#x2F;etc&#x2F;yum.repos.d&#x2F;epel.repo http:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;repo&#x2F;epel-7.repo</span><br><span class="line">#安装dnf工具</span><br><span class="line"></span><br><span class="line">yum install dnf -y</span><br><span class="line">dnf makecache</span><br><span class="line">#安装ntpdate工具</span><br><span class="line">dnf install ntpdate -y</span><br><span class="line">#同步阿里云时间</span><br><span class="line">ln -sf &#x2F;usr&#x2F;share&#x2F;zoneinfo&#x2F;Asia&#x2F;Shanghai &#x2F;etc&#x2F;localtime</span><br><span class="line">ntpdate ntp.aliyun.com</span><br></pre></td></tr></table></figure>

<p>在其他的节点执行此脚本跑一下即可。</p>
<h3 id="2-2、安装docker"><a href="#2-2、安装docker" class="headerlink" title="2.2、安装docker"></a>2.2、安装<code>docker</code></h3><h4 id="（1）添加docker软件yum源"><a href="#（1）添加docker软件yum源" class="headerlink" title="（1）添加docker软件yum源"></a>（1）添加<code>docker</code>软件<code>yum</code>源</h4><p>方法：浏览器打开<code>mirrors.aliyun.com</code>网站，找到<code>docker-ce</code>，即可看到镜像仓库源</p>
<p><img src="http://iubest.gitee.io/pic/640-1601003120653.webp" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">~]# curl -o &#x2F;etc&#x2F;yum.repos.d&#x2F;docker-ce.repo https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;docker-ce&#x2F;linux&#x2F;centos&#x2F;docker-ce.repo</span><br><span class="line">~]# cat &#x2F;etc&#x2F;yum.repos.d&#x2F;docker-ce.repo</span><br><span class="line">[docker-ce-stable]</span><br><span class="line">name&#x3D;Docker CE Stable - $basearch</span><br><span class="line">baseurl&#x3D;https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;docker-ce&#x2F;linux&#x2F;centos&#x2F;7&#x2F;$basearch&#x2F;stable</span><br><span class="line">enabled&#x3D;1</span><br><span class="line">gpgcheck&#x3D;1</span><br><span class="line">gpgkey&#x3D;https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;docker-ce&#x2F;linux&#x2F;centos&#x2F;gpg</span><br><span class="line">...</span><br><span class="line">...</span><br></pre></td></tr></table></figure>

<h4 id="（2）安装docker-ce组件"><a href="#（2）安装docker-ce组件" class="headerlink" title="（2）安装docker-ce组件"></a>（2）安装<code>docker-ce</code>组件</h4><p>列出所有可以安装的版本</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">~]# dnf list docker-ce --showduplicates</span><br><span class="line">docker-ce.x86_64       3:18.09.6-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:18.09.7-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:18.09.8-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:18.09.9-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:19.03.0-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:19.03.1-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:19.03.2-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:19.03.3-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:19.03.4-3.el7               docker-ce-stable</span><br><span class="line">docker-ce.x86_64       3:19.03.5-3.el7               docker-ce-stable</span><br><span class="line">.....</span><br></pre></td></tr></table></figure>

<p>这里我们安装最新版本的<code>docker</code>，所有的节点都需要安装<code>docker</code>服务</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# dnf install -y  docker-ce docker-ce-cli</span><br></pre></td></tr></table></figure>

<h4 id="（3）启动docker并设置开机自启动"><a href="#（3）启动docker并设置开机自启动" class="headerlink" title="（3）启动docker并设置开机自启动"></a>（3）启动<code>docker</code>并设置开机自启动</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# systemctl enable --now docker</span><br></pre></td></tr></table></figure>

<p>查看版本号，检测<code>docker</code>是否安装成功</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">~]# docker --version</span><br><span class="line">Docker version 19.03.12, build 48a66213fea</span><br></pre></td></tr></table></figure>

<p>上面的这种查看<code>docker client</code>的版本的。建议使用下面这种方法查看<code>docker-ce</code>版本号，这种方法把<code>docker</code>的<code>client</code>端和<code>server</code>端的版本号查看的一清二楚。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">~]# docker version</span><br><span class="line">Client:</span><br><span class="line"> Version:           19.03.12</span><br><span class="line"> API version:       1.40</span><br><span class="line"> Go version:        go1.13.10</span><br><span class="line"> Git commit:        039a7df9ba</span><br><span class="line"> Built:             Wed Sep  4 16:51:21 2019</span><br><span class="line"> OS&#x2F;Arch:           linux&#x2F;amd64</span><br><span class="line"> Experimental:      false</span><br><span class="line"></span><br><span class="line">Server: Docker Engine - Community</span><br><span class="line"> Engine:</span><br><span class="line">  Version:          19.03.12</span><br><span class="line">  API version:      1.40 (minimum version 1.12)</span><br><span class="line">  Go version:       go1.13.10</span><br><span class="line">  Git commit:       039a7df</span><br><span class="line">  Built:            Wed Sep  4 16:22:32 2019</span><br><span class="line">  OS&#x2F;Arch:          linux&#x2F;amd64</span><br><span class="line">  Experimental:     false</span><br></pre></td></tr></table></figure>

<h4 id="（4）更换docker的镜像仓库源"><a href="#（4）更换docker的镜像仓库源" class="headerlink" title="（4）更换docker的镜像仓库源"></a>（4）更换<code>docker</code>的镜像仓库源</h4><p>默认的镜像仓库地址是<code>docker</code>官方的，国内访问异常缓慢，因此更换为个人阿里云的源。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">~]# cat &gt; &#x2F;etc&#x2F;docker&#x2F;daemon.json &lt;&lt; EOF</span><br><span class="line">&#123;</span><br><span class="line">  &quot;registry-mirrors&quot;: [&quot;https:&#x2F;&#x2F;f1bhsuge.mirror.aliyuncs.com&quot;]</span><br><span class="line">&#125;</span><br><span class="line">EOF</span><br></pre></td></tr></table></figure>

<p>由于重新加载<code>docker仓库源</code>，所以需要重启<code>docker</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# systemctl restart docker</span><br></pre></td></tr></table></figure>

<h3 id="2-3、安装kubernetes"><a href="#2-3、安装kubernetes" class="headerlink" title="2.3、安装kubernetes"></a>2.3、安装<code>kubernetes</code></h3><h4 id="（1）添加kubernetes软件yum源"><a href="#（1）添加kubernetes软件yum源" class="headerlink" title="（1）添加kubernetes软件yum源"></a>（1）添加<code>kubernetes</code>软件<code>yum</code>源</h4><p>方法：浏览器打开<code>mirrors.aliyun.com</code>网站，找到<code>kubernetes</code>，即可看到镜像仓库源</p>
<p><img src="http://iubest.gitee.io/pic/640-1601003120790.webp" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">~]# cat &gt; &#x2F;etc&#x2F;yum.repos.d&#x2F;kubernetes.repo &lt;&lt; EOF</span><br><span class="line">[kubernetes]</span><br><span class="line">name&#x3D;Kubernetes</span><br><span class="line">baseurl&#x3D;https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;kubernetes&#x2F;yum&#x2F;repos&#x2F;kubernetes-el7-x86_64</span><br><span class="line">enabled&#x3D;1</span><br><span class="line">gpgcheck&#x3D;0</span><br><span class="line">repo_gpgcheck&#x3D;0</span><br><span class="line">gpgkey&#x3D;https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;kubernetes&#x2F;yum&#x2F;doc&#x2F;yum-key.gpg https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;kubernetes&#x2F;yum&#x2F;doc&#x2F;rpm-package-key.gpg</span><br><span class="line">EOF</span><br></pre></td></tr></table></figure>

<p>最好是重新生成缓存</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">~]# dnf clean all</span><br><span class="line">~]# dnf makecache</span><br></pre></td></tr></table></figure>

<h4 id="（2）安装kubeadm、kubelet和kubectl组件"><a href="#（2）安装kubeadm、kubelet和kubectl组件" class="headerlink" title="（2）安装kubeadm、kubelet和kubectl组件"></a>（2）安装<code>kubeadm</code>、<code>kubelet</code>和<code>kubectl</code>组件</h4><p>所有的节点都需要安装这几个组件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# dnf list kubeadm --showduplicates</span><br><span class="line">kubeadm.x86_64                       1.17.7-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.17.7-1                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.17.8-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.17.9-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.18.0-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.18.1-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.18.2-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.18.3-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.18.4-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.18.4-1                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.18.5-0                     kubernetes</span><br><span class="line">kubeadm.x86_64                       1.18.6-0                     kubernetes</span><br></pre></td></tr></table></figure>

<p>由于kubernetes版本变更非常快，因此列出有哪些版本，选择一个合适的。我们这里安装<code>1.18.6</code>版本。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# dnf install -y kubelet-1.18.6 kubeadm-1.18.6 kubectl-1.18.6</span><br></pre></td></tr></table></figure>

<h4 id="（3）设置开机自启动"><a href="#（3）设置开机自启动" class="headerlink" title="（3）设置开机自启动"></a>（3）设置开机自启动</h4><blockquote>
<p>我们先设置开机自启，但是<code>kubelet</code>服务暂时先不启动。</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# systemctl enable kubelet</span><br></pre></td></tr></table></figure>

<h3 id="2-4、Haproxy-Keepalived配置高可用VIP"><a href="#2-4、Haproxy-Keepalived配置高可用VIP" class="headerlink" title="2.4、Haproxy+Keepalived配置高可用VIP"></a>2.4、<code>Haproxy+Keepalived</code>配置高可用VIP</h3><p>高可用我们采用官方推荐的<code>HAproxy+Keepalived</code>，<code>HAproxy</code>和<code>Keepalived</code>以守护进程的方式在所有<code>Master</code>节点部署。</p>
<h4 id="（1）安装keepalived和haproxy"><a href="#（1）安装keepalived和haproxy" class="headerlink" title="（1）安装keepalived和haproxy"></a>（1）安装<code>keepalived</code>和<code>haproxy</code></h4><p>注意：只需要在三个<code>master</code>节点安装即可</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# dnf install -y keepalived haproxy </span><br></pre></td></tr></table></figure>

<h4 id="（2）配置Haproxy服务"><a href="#（2）配置Haproxy服务" class="headerlink" title="（2）配置Haproxy服务"></a>（2）配置<code>Haproxy</code>服务</h4><p>所有<code>master</code>节点的<code>haproxy</code>配置相同，haproxy的配置文件是<code>/etc/haproxy/haproxy.cfg</code>。<code>master1</code>节点配置完成之后再分发给<code>master2、master3</code>两个节点。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">global</span><br><span class="line">  maxconn  2000</span><br><span class="line">  ulimit-n  16384</span><br><span class="line">  log  127.0.0.1 local0 err</span><br><span class="line">  stats timeout 30s</span><br><span class="line"></span><br><span class="line">defaults</span><br><span class="line">  log global</span><br><span class="line">  mode  http</span><br><span class="line">  option  httplog</span><br><span class="line">  timeout connect 5000</span><br><span class="line">  timeout client  50000</span><br><span class="line">  timeout server  50000</span><br><span class="line">  timeout http-request 15s</span><br><span class="line">  timeout http-keep-alive 15s</span><br><span class="line"></span><br><span class="line">frontend monitor-in</span><br><span class="line">  bind *:33305</span><br><span class="line">  mode http</span><br><span class="line">  option httplog</span><br><span class="line">  monitor-uri &#x2F;monitor</span><br><span class="line"></span><br><span class="line">listen stats</span><br><span class="line">  bind    *:8006</span><br><span class="line">  mode    http</span><br><span class="line">  stats   enable</span><br><span class="line">  stats   hide-version</span><br><span class="line">  stats   uri       &#x2F;stats</span><br><span class="line">  stats   refresh   30s</span><br><span class="line">  stats   realm     Haproxy\ Statistics</span><br><span class="line">  stats   auth      admin:admin</span><br><span class="line"></span><br><span class="line">frontend k8s-master</span><br><span class="line">  bind 0.0.0.0:8443</span><br><span class="line">  bind 127.0.0.1:8443</span><br><span class="line">  mode tcp</span><br><span class="line">  option tcplog</span><br><span class="line">  tcp-request inspect-delay 5s</span><br><span class="line">  default_backend k8s-master</span><br><span class="line"></span><br><span class="line">backend k8s-master</span><br><span class="line">  mode tcp</span><br><span class="line">  option tcplog</span><br><span class="line">  option tcp-check</span><br><span class="line">  balance roundrobin</span><br><span class="line">  default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100</span><br><span class="line">  server master1 192.168.50.128:6443  check inter 2000 fall 2 rise 2 weight 100</span><br><span class="line">  server master2 192.168.50.129:6443  check inter 2000 fall 2 rise 2 weight 100</span><br><span class="line">  server master3 192.168.50.130:6443  check inter 2000 fall 2 rise 2 weight 100</span><br></pre></td></tr></table></figure>

<p>注意这里的三个master节点的ip地址要根据你自己的情况配置好。</p>
<h4 id="（3）配置Keepalived服务"><a href="#（3）配置Keepalived服务" class="headerlink" title="（3）配置Keepalived服务"></a>（3）配置<code>Keepalived</code>服务</h4><p><code>keepalived</code>中使用<code>track_script</code>机制来配置脚本进行探测<code>kubernetes</code>的<code>master</code>节点是否宕机，并以此切换节点实现高可用。</p>
<p><code>master1</code>节点的<code>keepalived</code>配置文件如下所示，配置文件所在的位置<code>/etc/keepalived/keepalived.cfg</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">! Configuration File for keepalived</span><br><span class="line">global_defs &#123;</span><br><span class="line">    router_id LVS_DEVEL</span><br><span class="line">&#125;</span><br><span class="line">vrrp_script chk_kubernetes &#123;</span><br><span class="line">    script &quot;&#x2F;etc&#x2F;keepalived&#x2F;check_kubernetes.sh&quot;</span><br><span class="line">    interval 2</span><br><span class="line">    weight -5</span><br><span class="line">    fall 3  </span><br><span class="line">    rise 2</span><br><span class="line">&#125;</span><br><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    state MASTER</span><br><span class="line">    interface ens33</span><br><span class="line">    mcast_src_ip 192.168.50.128</span><br><span class="line">    virtual_router_id 51</span><br><span class="line">    priority 100</span><br><span class="line">    advert_int 2</span><br><span class="line">    authentication &#123;</span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass K8SHA_KA_AUTH</span><br><span class="line">    &#125;</span><br><span class="line">    virtual_ipaddress &#123;</span><br><span class="line">        192.168.50.100</span><br><span class="line">    &#125;</span><br><span class="line">#    track_script &#123;</span><br><span class="line">#       chk_kubernetes</span><br><span class="line">#    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>需要注意几点（前两点记得修改）：</p>
<ul>
<li><code>mcast_src_ip</code>：配置多播源地址，此地址是当前主机的ip地址。</li>
<li><code>priority</code>：<code>keepalived</code>根据此项参数的大小仲裁<code>master</code>节点。我们这里让master节点为<code>kubernetes</code>提供服务，其他两个节点暂时为备用节点。因此<code>master1</code>节点设置为<code>100</code>，<code>master2</code>节点设置为<code>99</code>，<code>master3</code>节点设置为<code>98</code>。</li>
<li><code>state</code>：我们将<code>master1</code>节点的<code>state</code>字段设置为<code>MASTER</code>，其他两个节点字段修改为<code>BACKUP</code>。</li>
<li>上面的集群检查功能是关闭的，等到集群建立完成后再开启。</li>
</ul>
<h4 id="（4）配置健康检测脚本"><a href="#（4）配置健康检测脚本" class="headerlink" title="（4）配置健康检测脚本"></a>（4）配置健康检测脚本</h4><p>我这里将健康检测脚本放置在<code>/etc/keepalived</code>目录下，<code>check_kubernetes.sh</code>检测脚本如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">#!&#x2F;bin&#x2F;bash</span><br><span class="line">#****************************************************************#</span><br><span class="line"># ScriptName: check_kubernetes.sh</span><br><span class="line"># Author: boming</span><br><span class="line"># Create Date: 2020-06-23 22:19</span><br><span class="line">#***************************************************************#</span><br><span class="line"></span><br><span class="line">function chech_kubernetes() &#123;</span><br><span class="line"> for ((i&#x3D;0;i&lt;5;i++));do</span><br><span class="line">  apiserver_pid_id&#x3D;$(pgrep kube-apiserver)</span><br><span class="line">  if [[ ! -z $apiserver_pid_id ]];then</span><br><span class="line">   return</span><br><span class="line">  else</span><br><span class="line">   sleep 2</span><br><span class="line">  fi</span><br><span class="line">  apiserver_pid_id&#x3D;0</span><br><span class="line"> done</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 1:running  0:stopped</span><br><span class="line">check_kubernetes</span><br><span class="line">if [[ $apiserver_pid_id -eq 0 ]];then</span><br><span class="line"> &#x2F;usr&#x2F;bin&#x2F;systemctl stop keepalived</span><br><span class="line"> exit 1</span><br><span class="line">else</span><br><span class="line"> exit 0</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>

<p>根据上面的注意事项配置<code>master2</code>、<code>master3</code>节点的<code>keepalived</code>服务。</p>
<h4 id="（5）启动Keeplived和Haproxy服务"><a href="#（5）启动Keeplived和Haproxy服务" class="headerlink" title="（5）启动Keeplived和Haproxy服务"></a>（5）启动<code>Keeplived</code>和<code>Haproxy</code>服务</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# systemctl enable --now keepalived haproxy</span><br></pre></td></tr></table></figure>

<p>确保万一，查看一下服务状态</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">~]# systemctl status keepalived haproxy</span><br><span class="line">~]# ping 192.168.50.100                    #检测一下是否通</span><br><span class="line">PING 192.168.50.100 (192.168.50.100) 56(84) bytes of data.</span><br><span class="line">64 bytes from 192.168.50.100: icmp_seq&#x3D;1 ttl&#x3D;64 time&#x3D;0.778 ms</span><br><span class="line">64 bytes from 192.168.50.100: icmp_seq&#x3D;2 ttl&#x3D;64 time&#x3D;0.339 ms</span><br></pre></td></tr></table></figure>

<h3 id="2-5、部署Master节点"><a href="#2-5、部署Master节点" class="headerlink" title="2.5、部署Master节点"></a>2.5、部署<code>Master</code>节点</h3><h4 id="（1）生成预处理文件"><a href="#（1）生成预处理文件" class="headerlink" title="（1）生成预处理文件"></a>（1）生成预处理文件</h4><p>在<code>master</code>节点执行如下指令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubeadm config print init-defaults &gt; kubeadm-init.yaml</span><br></pre></td></tr></table></figure>

<p>这个文件<code>kubeadm-init.yaml</code>，是我们初始化使用的文件，里面大概修改这几项参数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# cat kubeadm-init.yaml </span><br><span class="line">apiVersion: kubeadm.k8s.io&#x2F;v1beta2</span><br><span class="line">bootstrapTokens:</span><br><span class="line">- groups:</span><br><span class="line">  - system:bootstrappers:kubeadm:default-node-token</span><br><span class="line">  token: abcdef.0123456789abcdef</span><br><span class="line">  ttl: 24h0m0s</span><br><span class="line">  usages:</span><br><span class="line">  - signing</span><br><span class="line">  - authentication</span><br><span class="line">kind: InitConfiguration</span><br><span class="line">localAPIEndpoint:</span><br><span class="line">  advertiseAddress: 192.168.50.100      #VIP的地址</span><br><span class="line">  bindPort:  6443</span><br><span class="line">nodeRegistration:</span><br><span class="line">  criSocket: &#x2F;var&#x2F;run&#x2F;dockershim.sock</span><br><span class="line">  name: master1</span><br><span class="line">  taints:</span><br><span class="line">  - effect: NoSchedule</span><br><span class="line">    key: node-role.kubernetes.io&#x2F;master</span><br><span class="line">---</span><br><span class="line">apiServer:            #添加如下两行信息</span><br><span class="line">  certSANs:</span><br><span class="line">  - &quot;192.168.50.100&quot;         #VIP地址</span><br><span class="line">  timeoutForControlPlane: 4m0s</span><br><span class="line">apiVersion: kubeadm.k8s.io&#x2F;v1beta2</span><br><span class="line">certificatesDir: &#x2F;etc&#x2F;kubernetes&#x2F;pki</span><br><span class="line">clusterName: kubernetes</span><br><span class="line">controllerManager: &#123;&#125;</span><br><span class="line">dns:</span><br><span class="line">  type: CoreDNS</span><br><span class="line">etcd:</span><br><span class="line">  local:</span><br><span class="line">    dataDir: &#x2F;var&#x2F;lib&#x2F;etcd</span><br><span class="line">imageRepository: registry.cn-hangzhou.aliyuncs.com&#x2F;google_containers   #阿里云的镜像站点</span><br><span class="line">controlPlaneEndpoint: &quot;192.168.50.100:8443&quot;    #VIP的地址和端口</span><br><span class="line">kind: ClusterConfiguration</span><br><span class="line">kubernetesVersion: v1.18.3        #kubernetes版本号</span><br><span class="line">networking:</span><br><span class="line">  dnsDomain: cluster.local </span><br><span class="line">  serviceSubnet: 10.96.0.0&#x2F;12       #选择默认即可，当然也可以自定义CIDR</span><br><span class="line">  podSubnet: 10.244.0.0&#x2F;16        #添加pod网段</span><br><span class="line">scheduler: &#123;&#125;</span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong>上面的<code>advertiseAddress</code>字段的值，这个值并非当前主机的网卡地址，而是高可用集群的<code>VIP</code>的地址。</p>
<p><strong>注意：</strong>上面的<code>controlPlaneEndpoint</code>这里填写的是<code>VIP</code>的地址，而端口则是<code>haproxy</code>服务的<code>8443</code>端口，也就是我们在<code>haproxy</code>里面配置的这段信息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">frontend k8s-master</span><br><span class="line">  bind 0.0.0.0:8443</span><br><span class="line">  bind 127.0.0.1:8443</span><br><span class="line">  mode tcp</span><br></pre></td></tr></table></figure>

<p>这一段里面的<code>8443</code>端，如果你自定义了其他端口，这里请记得修改<code>controlPlaneEndpoint</code>里面的端口。</p>
<h4 id="（2）提前拉取镜像"><a href="#（2）提前拉取镜像" class="headerlink" title="（2）提前拉取镜像"></a>（2）提前拉取镜像</h4><p>如果直接采用<code>kubeadm init</code>来初始化，中间会有系统自动拉取镜像的这一步骤，这是比较慢的，我建议分开来做，所以这里就先提前拉取镜像。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubeadm config images pull --config kubeadm-init.yaml</span><br><span class="line">[config&#x2F;images] Pulled registry.aliyuncs.com&#x2F;google_containers&#x2F;kube-apiserver:v1.18.0</span><br><span class="line">[config&#x2F;images] Pulled registry.aliyuncs.com&#x2F;google_containers&#x2F;kube-controller-manager:v1.18.0</span><br><span class="line">[config&#x2F;images] Pulled registry.aliyuncs.com&#x2F;google_containers&#x2F;kube-scheduler:v1.18.0</span><br><span class="line">[config&#x2F;images] Pulled registry.aliyuncs.com&#x2F;google_containers&#x2F;kube-proxy:v1.18.0</span><br><span class="line">[config&#x2F;images] Pulled registry.aliyuncs.com&#x2F;google_containers&#x2F;pause:3.1</span><br><span class="line">[config&#x2F;images] Pulled registry.aliyuncs.com&#x2F;google_containers&#x2F;etcd:3.4.3-0</span><br><span class="line">[config&#x2F;images] Pulled registry.aliyuncs.com&#x2F;google_containers&#x2F;coredns:1.6.5</span><br></pre></td></tr></table></figure>

<p>如果大家看到开头的两行<code>warning</code>信息（我这里没有打印），不必担心，这只是警告，不影响我们完成实验。</p>
<h5 id="其他master节点提前拉取镜像"><a href="#其他master节点提前拉取镜像" class="headerlink" title="其他master节点提前拉取镜像"></a>其他<code>master</code>节点提前拉取镜像</h5><p>其他两个<code>master</code>节点在初始化之前也尽量先把镜像拉取下来，这样子减少初始化时间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# scp kubeadm-init.yaml root@master2:~</span><br><span class="line">[root@master1 ~]# scp kubeadm-init.yaml root@master3:~</span><br></pre></td></tr></table></figure>

<p><strong><code>master2</code>节点</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 注意在master2节点执行如下命令</span><br><span class="line">[root@master2 ~]# kubeadm config images pull --config kubeadm-init.yaml</span><br></pre></td></tr></table></figure>

<p><strong><code>master3</code>节点</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 注意在master3节点执行如下命令</span><br><span class="line">[root@master3 ~]# kubeadm config images pull --config kubeadm-init.yaml</span><br></pre></td></tr></table></figure>

<h4 id="（3）初始化kubenetes的master1节点"><a href="#（3）初始化kubenetes的master1节点" class="headerlink" title="（3）初始化kubenetes的master1节点"></a>（3）初始化<code>kubenetes</code>的<code>master1</code>节点</h4><p>执行如下命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubeadm init --config kubeadm-init.yaml --upload-certs</span><br><span class="line">[init] Using Kubernetes version: v1.18.3</span><br><span class="line">[preflight] Running pre-flight checks</span><br><span class="line"> [WARNING IsDockerSystemdCheck]: detected &quot;cgroupfs&quot; as the Docker cgroup driver. The recommended driver is &quot;systemd&quot;. Please follow the guide at https:&#x2F;&#x2F;kubernetes.io&#x2F;docs&#x2F;setup&#x2F;cri&#x2F;</span><br><span class="line">[preflight] Pulling images required for setting up a Kubernetes cluster</span><br><span class="line">[preflight] This might take a minute or two, depending on the speed of your internet connection</span><br><span class="line">[preflight] You can also perform this action in beforehand using &#39;kubeadm config images pull&#39;</span><br><span class="line">[kubelet-start] Writing kubelet environment file with flags to file &quot;&#x2F;var&#x2F;lib&#x2F;kubelet&#x2F;kubeadm-flags.env&quot;</span><br><span class="line">[kubelet-start] Writing kubelet configuration to file &quot;&#x2F;var&#x2F;lib&#x2F;kubelet&#x2F;config.yaml&quot;</span><br><span class="line">[certs] apiserver serving cert is signed for DNS names [master1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.50.128 192.168.50.100]</span><br><span class="line">...           # 省略</span><br><span class="line">[upload-certs] Skipping phase. Please see --upload-certs</span><br><span class="line">[mark-control-plane] Marking the node master1 as control-plane by adding the label &quot;node-role.kubernetes.io&#x2F;master&#x3D;&#39;&#39;&quot;</span><br><span class="line">[mark-control-plane] Marking the node master1 as control-plane by adding the taints [node-role.kubernetes.io&#x2F;master:NoSchedule]</span><br><span class="line">[addons] Applied essential addon: CoreDNS</span><br><span class="line">[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address</span><br><span class="line">[addons] Applied essential addon: kube-proxy</span><br><span class="line"></span><br><span class="line">Your Kubernetes control-plane has initialized successfully!</span><br><span class="line"></span><br><span class="line">To start using your cluster, you need to run the following as a regular user:</span><br><span class="line"></span><br><span class="line">  mkdir -p $HOME&#x2F;.kube</span><br><span class="line">  sudo cp -i &#x2F;etc&#x2F;kubernetes&#x2F;admin.conf $HOME&#x2F;.kube&#x2F;config</span><br><span class="line">  sudo chown $(id -u):$(id -g) $HOME&#x2F;.kube&#x2F;config</span><br><span class="line"></span><br><span class="line">You should now deploy a pod network to the cluster.</span><br><span class="line">Run &quot;kubectl apply -f [podnetwork].yaml&quot; with one of the options listed at:</span><br><span class="line">  https:&#x2F;&#x2F;kubernetes.io&#x2F;docs&#x2F;concepts&#x2F;cluster-administration&#x2F;addons&#x2F;</span><br><span class="line"></span><br><span class="line">You can now join any number of control-plane nodes by copying certificate authorities</span><br><span class="line">and service account keys on each node and then running the following as root:</span><br><span class="line"></span><br><span class="line">  kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">    --discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648 \</span><br><span class="line">    --control-plane --certificate-key 4931f39d3f53351cb6966a9dcc53cb5cbd2364c6d5b83e50e258c81fbec69539 </span><br><span class="line"></span><br><span class="line">Then you can join any number of worker nodes by running the following on each as root:</span><br><span class="line"></span><br><span class="line">kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">    --discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648</span><br></pre></td></tr></table></figure>

<p>这个过程大概<code>30s</code>的时间就做完了，之所以初始化的这么快就是因为我们提前拉取了镜像。像我上面这样的没有报错信息，并且显示上面的最后10行类似的信息这些，说明我们的<code>master1</code>节点是初始化成功的。</p>
<p>在使用集群之前还需要做些收尾工作，在<code>master1</code>节点执行：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# mkdir -p $HOME&#x2F;.kube</span><br><span class="line">[root@master1 ~]# sudo cp -i &#x2F;etc&#x2F;kubernetes&#x2F;admin.conf $HOME&#x2F;.kube&#x2F;config</span><br><span class="line">[root@master1 ~]# sudo chown $(id -u):$(id -g) $HOME&#x2F;.kube&#x2F;config</span><br></pre></td></tr></table></figure>

<p>再配置一下环境变量</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# cat &gt;&gt; ~&#x2F;.bashrc &lt;&lt;EOF</span><br><span class="line">export KUBECONFIG&#x3D;&#x2F;etc&#x2F;kubernetes&#x2F;admin.conf</span><br><span class="line">EOF</span><br><span class="line">[root@master1 ~]# source ~&#x2F;.bashrc</span><br></pre></td></tr></table></figure>

<p>好了，此时的<code>master1</code>节点就算是初始化完毕了。</p>
<p>有个重要的点就是最后几行信息中，其中有两条<code>kubeadm join 192.168.50.100:8443</code> 开头的信息。这分别是其他<code>master</code>节点和<code>node</code>节点加入<code>kubernetes</code>集群的认证命令。这个密钥是系统根据<code>sha256</code>算法计算出来的，必须持有这样的密钥才可以加入当前的<code>kubernetes</code>集群。</p>
<h5 id="使用区别"><a href="#使用区别" class="headerlink" title="使用区别"></a>使用区别</h5><p>这两条加入集群的命令是有一些区别的：</p>
<p>比如这个第一条，我们看到最后有一行内容<code>--control-plane --certificate-key xxxx</code>，这是控制节点加入集群的命令，控制节点是<code>kubernetes</code>官方的说法，其实在我们这里指的就是其他的<code>master</code>节点。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">    --discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648 \</span><br><span class="line">    --control-plane --certificate-key 4931f39d3f53351cb6966a9dcc53cb5cbd2364c6d5b83e50e258c81fbec69539</span><br></pre></td></tr></table></figure>

<p>而最后一条就表示<code>node</code>节点加入集群的命令，比如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">    --discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648</span><br></pre></td></tr></table></figure>

<p>所以这两个节点使用时要看清楚是什么类型的节点加入集群的。</p>
<h5 id="查看节点"><a href="#查看节点" class="headerlink" title="查看节点"></a>查看节点</h5><p>如果此时查看当前集群的节点，会发现只有<code>master1</code>节点自己。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl get node</span><br><span class="line">NAME      STATUS     ROLES    AGE     VERSION</span><br><span class="line">master1   NotReady   master   9m58s   v1.18.4</span><br></pre></td></tr></table></figure>

<p>接下来我们把其他两个<code>master</code>节点加入到<code>kubernetes</code>集群中</p>
<h3 id="2-6、其他master节点加入kubernetes集群中"><a href="#2-6、其他master节点加入kubernetes集群中" class="headerlink" title="2.6、其他master节点加入kubernetes集群中"></a>2.6、其他<code>master</code>节点加入<code>kubernetes</code>集群中</h3><h4 id="（1）master2节点加入集群"><a href="#（1）master2节点加入集群" class="headerlink" title="（1）master2节点加入集群"></a>（1）<code>master2</code>节点加入集群</h4><p>既然是其他的master节点加入集群，那肯定是使用如下命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">[root@master2 ~]#  kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">     --discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648 \</span><br><span class="line">     --control-plane --certificate-key 4931f39d3f53351cb6966a9dcc53cb5cbd2364c6d5b83e50e258c81fbec69539</span><br><span class="line"></span><br><span class="line">[preflight] Running pre-flight checks</span><br><span class="line"> [WARNING IsDockerSystemdCheck]: detected &quot;cgroupfs&quot; as the Docker cgroup driver. The </span><br><span class="line">......                                  #省略若干</span><br><span class="line">[mark-control-plane] Marking the node master2 as control-plane by adding the taints [node-role.kubernetes.io&#x2F;master:NoSchedule]</span><br><span class="line"></span><br><span class="line">This node has joined the cluster and a new control plane instance was created:</span><br><span class="line"></span><br><span class="line">To start administering your cluster from this node, you need to run the following as a regular user:</span><br><span class="line"></span><br><span class="line"> mkdir -p $HOME&#x2F;.kube</span><br><span class="line"> sudo cp -i &#x2F;etc&#x2F;kubernetes&#x2F;admin.conf $HOME&#x2F;.kube&#x2F;config</span><br><span class="line"> sudo chown $(id -u):$(id -g) $HOME&#x2F;.kube&#x2F;config</span><br><span class="line"></span><br><span class="line">Run &#39;kubectl get nodes&#39; to see this node join the cluster.</span><br></pre></td></tr></table></figure>

<p>看上去没有报错，说明加入集群成功，现在再执行一些收尾工作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@master2 ~]# mkdir -p $HOME&#x2F;.kube</span><br><span class="line">[root@master2 ~]# sudo cp -i &#x2F;etc&#x2F;kubernetes&#x2F;admin.conf $HOME&#x2F;.kube&#x2F;config</span><br><span class="line">[root@master2 ~]# sudo chown $(id -u):$(id -g) $HOME&#x2F;.kube&#x2F;config</span><br></pre></td></tr></table></figure>

<p>加环境变量</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@master2 ~]# cat &gt;&gt; ~&#x2F;.bashrc &lt;&lt;EOF</span><br><span class="line">export KUBECONFIG&#x3D;&#x2F;etc&#x2F;kubernetes&#x2F;admin.conf</span><br><span class="line">EOF</span><br><span class="line">[root@master2 ~]# source ~&#x2F;.bashrc</span><br></pre></td></tr></table></figure>

<h4 id="（2）master3节点加入集群"><a href="#（2）master3节点加入集群" class="headerlink" title="（2）master3节点加入集群"></a>（2）<code>master3</code>节点加入集群</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@master3 ~]#  kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">     --discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648 \</span><br><span class="line">     --control-plane --certificate-key 4931f39d3f53351cb6966a9dcc53cb5cbd2364c6d5b83e50e258c81fbec69539</span><br></pre></td></tr></table></figure>

<p>做一些收尾工作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@master3 ~]# mkdir -p $HOME&#x2F;.kube</span><br><span class="line">[root@master3 ~]# sudo cp -i &#x2F;etc&#x2F;kubernetes&#x2F;admin.conf $HOME&#x2F;.kube&#x2F;config</span><br><span class="line">[root@master3 ~]# sudo chown $(id -u):$(id -g) $HOME&#x2F;.kube&#x2F;config</span><br><span class="line">[root@master3 ~]# cat &gt;&gt; ~&#x2F;.bashrc &lt;&lt;EOF</span><br><span class="line">export KUBECONFIG&#x3D;&#x2F;etc&#x2F;kubernetes&#x2F;admin.conf</span><br><span class="line">EOF</span><br><span class="line">[root@master3 ~]# source ~&#x2F;.bashrc</span><br></pre></td></tr></table></figure>

<p>到此，所有的<code>master</code>节点都已经加入集群</p>
<h5 id="查看集群master节点"><a href="#查看集群master节点" class="headerlink" title="查看集群master节点"></a>查看集群<code>master</code>节点</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl get node</span><br><span class="line">NAME      STATUS     ROLES    AGE     VERSION</span><br><span class="line">master1   NotReady   master   25m     v1.18.4</span><br><span class="line">master2   NotReady   master   12m     v1.18.4</span><br><span class="line">master3   NotReady   master   3m30s   v1.18.4</span><br></pre></td></tr></table></figure>

<p>你可以在任意一个<code>master</code>节点上执行<code>kubectl get node</code>查看集群节点的命令。</p>
<h3 id="2-7、node节点加入kubernetes集群中"><a href="#2-7、node节点加入kubernetes集群中" class="headerlink" title="2.7、node节点加入kubernetes集群中"></a>2.7、<code>node</code>节点加入<code>kubernetes</code>集群中</h3><p>正如我们上面所说的，<code>master1</code>节点初始化完成后，第二条<code>kubeadm join xxx</code>（或者说是最后一行内容）内容便是<code>node</code>节点加入集群的命令。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">~]# kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">    --discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648</span><br></pre></td></tr></table></figure>

<p>注意：<code>node</code>节点加入集群只需要执行上面的一条命令即可，只要没有报错就表示成功。不必像<code>master</code>一样做最后的加入环境变量等收尾工作。</p>
<h4 id="（1）node1节点加入集群"><a href="#（1）node1节点加入集群" class="headerlink" title="（1）node1节点加入集群"></a>（1）<code>node1</code>节点加入集群</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">[root@node1 ~]# kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">&gt;     --discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648</span><br><span class="line">[preflight] Running pre-flight checks</span><br><span class="line"> [WARNING IsDockerSystemdCheck]: detected &quot;cgroupfs&quot; as the Docker cgroup driver. The recommended driver is &quot;systemd&quot;. Please follow the guide at https:&#x2F;&#x2F;kubernetes.io&#x2F;docs&#x2F;setup&#x2F;cri&#x2F;</span><br><span class="line">[preflight] Reading configuration from the cluster...</span><br><span class="line">....</span><br><span class="line">....</span><br><span class="line">[kubelet-start] Writing kubelet environment file with flags to file &quot;&#x2F;var&#x2F;lib&#x2F;kubelet&#x2F;kubeadm-flags.env&quot;</span><br><span class="line">[kubelet-start] Starting the kubelet</span><br><span class="line">[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...</span><br><span class="line"></span><br><span class="line">This node has joined the cluster:</span><br><span class="line">* Certificate signing request was sent to apiserver and a response was received.</span><br><span class="line">* The Kubelet was informed of the new secure connection details.</span><br><span class="line"></span><br><span class="line">Run &#39;kubectl get nodes&#39; on the control-plane to see this node join the cluster.</span><br></pre></td></tr></table></figure>

<p>当看到倒数第四行内容<code>This node has joined the cluster</code>，这一行信息表示<code>node1</code>节点加入集群成功。</p>
<h4 id="（2）node2节点加入集群"><a href="#（2）node2节点加入集群" class="headerlink" title="（2）node2节点加入集群"></a>（2）<code>node2</code>节点加入集群</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@node2 ~]# kubeadm join 192.168.50.100:8443 --token abcdef.0123456789abcdef \</span><br><span class="line">--discovery-token-ca-cert-hash sha256:4c738bc8e2684c5d52d80687d48925613b66ab660403649145eb668d71d85648</span><br></pre></td></tr></table></figure>

<h4 id="（3）查看集群节点信息"><a href="#（3）查看集群节点信息" class="headerlink" title="（3）查看集群节点信息"></a>（3）查看集群节点信息</h4><p>此时我们可以在任意一个<code>master</code>节点执行如下命令查看此集群的节点信息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl get nodes</span><br><span class="line">NAME      STATUS     ROLES    AGE     VERSION</span><br><span class="line">master1   NotReady   master   20h     v1.18.4</span><br><span class="line">master2   NotReady   master   20h     v1.18.4</span><br><span class="line">master3   NotReady   master   20h     v1.18.4</span><br><span class="line">node1     NotReady   &lt;none&gt;   5m15s   v1.18.4</span><br><span class="line">node2     NotReady   &lt;none&gt;   5m11s   v1.18.4</span><br></pre></td></tr></table></figure>

<p>可以看到集群的五个节点都已经存在，但是现在还不能用，也就是说现在<strong>集群节点是不可用的</strong>，原因在于上面的第2个字段，我们看到五个节点都是``NotReady`状态，这是因为我们还没有安装网络插件。</p>
<p>网络插件有<code>calico</code>，<code>flannel</code>等插件，这里我们选择使用<code>flannel</code>插件。</p>
<h3 id="2-8、安装网络插件"><a href="#2-8、安装网络插件" class="headerlink" title="2.8、安装网络插件"></a>2.8、安装网络插件</h3><h4 id="（1）默认方法"><a href="#（1）默认方法" class="headerlink" title="（1）默认方法"></a>（1）默认方法</h4><p>默认大家从网上看的教程都会使用这个命令来初始化。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">~]# kubectl apply -f https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;coreos&#x2F;flannel&#x2F;master&#x2F;Documentation&#x2F;kube-flannel.yml</span><br></pre></td></tr></table></figure>

<p>事实上很多用户都不能成功，因为国内网络受限，所以可以这样子来做。</p>
<h4 id="（2）更换flannel镜像源"><a href="#（2）更换flannel镜像源" class="headerlink" title="（2）更换flannel镜像源"></a>（2）更换<code>flannel</code>镜像源</h4><p><code>master1</code>节点上修改本地的<code>hosts</code>文件添加如下内容以便解析</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">199.232.28.133  raw.githubusercontent.com</span><br></pre></td></tr></table></figure>

<p>然后下载<code>flannel</code>文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# curl -o kube-flannel.yml   https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;coreos&#x2F;flannel&#x2F;master&#x2F;Documentation&#x2F;kube-flannel.yml</span><br></pre></td></tr></table></figure>

<p>编辑镜像源，默认的镜像地址我们修改一下。把<code>yaml</code>文件中所有的<code>quay.io</code>修改为 <code>quay-mirror.qiniu.com</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# sed -i &#39;s&#x2F;quay.io&#x2F;quay-mirror.qiniu.com&#x2F;g&#39; kube-flannel.yml</span><br></pre></td></tr></table></figure>

<p>此时保存保存退出。在<code>master</code>节点执行此命令。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl apply -f kube-flannel.yml </span><br><span class="line">podsecuritypolicy.policy&#x2F;psp.flannel.unprivileged created</span><br><span class="line">clusterrole.rbac.authorization.k8s.io&#x2F;flannel created</span><br><span class="line">clusterrolebinding.rbac.authorization.k8s.io&#x2F;flannel created</span><br><span class="line">serviceaccount&#x2F;flannel created</span><br><span class="line">configmap&#x2F;kube-flannel-cfg created</span><br><span class="line">daemonset.apps&#x2F;kube-flannel-ds-amd64 created</span><br><span class="line">daemonset.apps&#x2F;kube-flannel-ds-arm64 created</span><br><span class="line">daemonset.apps&#x2F;kube-flannel-ds-arm created</span><br><span class="line">daemonset.apps&#x2F;kube-flannel-ds-ppc64le created</span><br><span class="line">daemonset.apps&#x2F;kube-flannel-ds-s390x created</span><br></pre></td></tr></table></figure>

<p>这样子就可以成功拉取<code>flannel</code>镜像了。当然你也可以使用我提供给大家的<code>kube-flannel.yml</code>文件</p>
<h5 id="查看flannel是否正常"><a href="#查看flannel是否正常" class="headerlink" title="查看flannel是否正常"></a>查看<code>flannel</code>是否正常</h5><p>如果你想查看<code>flannel</code>这些<code>pod</code>运行是否正常，使用如下命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl get pods -n kube-system | grep flannel</span><br><span class="line">NAME                              READY   STATUS    RESTARTS   AGE</span><br><span class="line">kube-flannel-ds-amd64-dp972       1&#x2F;1     Running   0          66s</span><br><span class="line">kube-flannel-ds-amd64-lkspx       1&#x2F;1     Running   0          66s</span><br><span class="line">kube-flannel-ds-amd64-rmsdk       1&#x2F;1     Running   0          66s</span><br><span class="line">kube-flannel-ds-amd64-wp668       1&#x2F;1     Running   0          66s</span><br><span class="line">kube-flannel-ds-amd64-zkrwh       1&#x2F;1     Running   0          66s</span><br></pre></td></tr></table></figure>

<p>如果第三字段<code>STATUS</code>不是处于<code>Running</code>状态的话，说明<code>flannel</code>是异常的，需要排查问题所在。</p>
<h5 id="查看节点是否为Ready"><a href="#查看节点是否为Ready" class="headerlink" title="查看节点是否为Ready"></a>查看节点是否为<code>Ready</code></h5><p>稍等片刻，执行如下指令查看节点是否可用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl get nodes</span><br><span class="line">NAME      STATUS   ROLES    AGE   VERSION</span><br><span class="line">master1   Ready    master   21h   v1.18.4</span><br><span class="line">master2   Ready    master   21h   v1.18.4</span><br><span class="line">master3   Ready    master   21h   v1.18.4</span><br><span class="line">node1     Ready    &lt;none&gt;   62m   v1.18.4</span><br><span class="line">node2     Ready    &lt;none&gt;   62m   v1.18.4</span><br></pre></td></tr></table></figure>

<p>目前节点状态是<code>Ready</code>，表示<strong>集群节点现在是可用的</strong>。</p>
<h2 id="3、测试kubernetes集群"><a href="#3、测试kubernetes集群" class="headerlink" title="3、测试kubernetes集群"></a>3、测试<code>kubernetes</code>集群</h2><h3 id="3-1、kubernetes集群测试"><a href="#3-1、kubernetes集群测试" class="headerlink" title="3.1、kubernetes集群测试"></a>3.1、<code>kubernetes</code>集群测试</h3><h4 id="（1）创建一个nginx的pod"><a href="#（1）创建一个nginx的pod" class="headerlink" title="（1）创建一个nginx的pod"></a>（1）创建一个<code>nginx</code>的<code>pod</code></h4><p>现在我们在<code>kubernetes</code>集群中创建一个<code>nginx</code>的<code>pod</code>，验证是否能正常运行。</p>
<p>在<code>master</code>节点执行一下步骤：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl create deployment nginx --image&#x3D;nginx</span><br><span class="line">deployment.apps&#x2F;nginx created</span><br><span class="line">[root@master1 ~]# kubectl expose deployment nginx --port&#x3D;80 --type&#x3D;NodePort</span><br><span class="line">service&#x2F;nginx exposed</span><br></pre></td></tr></table></figure>

<p>现在我们查看<code>pod</code>和<code>service</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl get pod,svc -o wide</span><br></pre></td></tr></table></figure>

<p><img src="http://iubest.gitee.io/pic/640-1601003120789.webp" alt="img"></p>
<p>打印的结果中，前半部分是<code>pod</code>相关信息，后半部分是<code>service</code>相关信息。我们看<code>service/nginx</code>这一行可以看出<code>service</code>暴漏给集群的端口是<code>30249</code>。记住这个端口。</p>
<p>然后从<code>pod</code>的详细信息可以看出此时<code>pod</code>在<code>node2</code>节点之上。<code>node2</code>节点的IP地址是<code>192.168.50.132</code></p>
<h4 id="（2）访问nginx验证集群"><a href="#（2）访问nginx验证集群" class="headerlink" title="（2）访问nginx验证集群"></a>（2）访问<code>nginx</code>验证集群</h4><p>那现在我们访问一下。打开浏览器(建议火狐浏览器)，访问地址就是：<code>http://192.168.50.132:30249</code></p>
<p><img src="http://iubest.gitee.io/pic/640-1601003120792.webp" alt="img"></p>
<h3 id="3-2、安装dashboard"><a href="#3-2、安装dashboard" class="headerlink" title="3.2、安装dashboard"></a>3.2、安装<code>dashboard</code></h3><h4 id="（1）创建dashboard"><a href="#（1）创建dashboard" class="headerlink" title="（1）创建dashboard"></a>（1）创建<code>dashboard</code></h4><p>先把<code>dashboard</code>的配置文件下载下来。由于我们之前已经添加了<code>hosts</code>解析，因此可以下载。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# wget https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;kubernetes&#x2F;dashboard&#x2F;v2.0.0-beta8&#x2F;aio&#x2F;deploy&#x2F;recommended.yaml</span><br></pre></td></tr></table></figure>

<p>默认<code>Dashboard</code>只能集群内部访问，修改<code>Service</code>为<code>NodePort</code>类型，暴露到外部：</p>
<p>大概在此文件的<code>32-44</code>行之间，修改为如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">kind: Service</span><br><span class="line">apiVersion: v1</span><br><span class="line">metadata:</span><br><span class="line">  labels:</span><br><span class="line">    k8s-app: kubernetes-dashboard</span><br><span class="line">  name: kubernetes-dashboard</span><br><span class="line">  namespace: kube-system</span><br><span class="line">spec:</span><br><span class="line">  type: NodePort       #加上此行</span><br><span class="line">  ports:</span><br><span class="line">    - port: 443</span><br><span class="line">      targetPort: 8443</span><br><span class="line">      nodePort: 30001     #加上此行，端口30001可以自行定义</span><br><span class="line">  selector:</span><br><span class="line">    k8s-app: kubernetes-dashboard</span><br></pre></td></tr></table></figure>

<h4 id="运行此yaml文件"><a href="#运行此yaml文件" class="headerlink" title="运行此yaml文件"></a>运行此<code>yaml</code>文件</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl apply -f recommended.yaml </span><br><span class="line">namespace&#x2F;kubernetes-dashboard created</span><br><span class="line">serviceaccount&#x2F;kubernetes-dashboard created</span><br><span class="line">service&#x2F;kubernetes-dashboard created</span><br><span class="line">secret&#x2F;kubernetes-dashboard-certs created</span><br><span class="line">...</span><br><span class="line">service&#x2F;dashboard-metrics-scraper created</span><br><span class="line">deployment.apps&#x2F;dashboard-metrics-scraper created</span><br></pre></td></tr></table></figure>

<h5 id="查看dashboard运行是否正常"><a href="#查看dashboard运行是否正常" class="headerlink" title="查看dashboard运行是否正常"></a>查看<code>dashboard</code>运行是否正常</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl get pods -n kubernetes-dashboard</span><br><span class="line">NAME                                         READY   STATUS    RESTARTS   AGE</span><br><span class="line">dashboard-metrics-scraper-694557449d-mlnl4   1&#x2F;1     Running   0          2m31s</span><br><span class="line">kubernetes-dashboard-9774cc786-ccvcf         1&#x2F;1     Running   0          2m31s</span><br></pre></td></tr></table></figure>

<p>主要是看<code>status</code>这一列的值，如果是<code>Running</code>，并且<code>RESTARTS</code>字段的值为<code>0</code>（只要这个值不是一直在渐渐变大），就是正常的，目前来看是没有问题的。我们可以继续下一步。</p>
<p>查看此<code>dashboard</code>的<code>pod</code>运行所在的节点</p>
<p><img src="http://iubest.gitee.io/pic/640-1601003120791.webp" alt="img"></p>
<p>从上面可以看出，<code>kubernetes-dashboard-9774cc786-ccvcf</code>运行所在的节点是<code>node2</code>上面，并且暴漏出来的端口是<code>30001</code>，所以访问地址是：<code>https://192.168.50.132:30001</code></p>
<p>用火狐浏览器访问，访问的时候会让输入<code>token</code>，从此处可以查看到<code>token</code>的值。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk &#39;&#x2F;dashboard-admin&#x2F;&#123;print $1&#125;&#39;)</span><br></pre></td></tr></table></figure>

<p><img src="http://iubest.gitee.io/pic/640-1601003120793.webp" alt="img"></p>
<p>把上面的<code>token</code>值输入进去即可进去<code>dashboard</code>界面。</p>
<p><img src="http://iubest.gitee.io/pic/640-1601003120794.webp" alt="img"></p>
<p>不过现在我们虽然可以登陆上去，但是我们权限不够还查看不了集群信息，因为我们还没有绑定集群角色，同学们可以先按照上面的尝试一下，再来做下面的步骤</p>
<h4 id="（2）cluster-admin管理员角色绑定"><a href="#（2）cluster-admin管理员角色绑定" class="headerlink" title="（2）cluster-admin管理员角色绑定"></a>（2）cluster-admin管理员角色绑定</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@master1 ~]# kubectl create serviceaccount dashboard-admin -n kube-system</span><br><span class="line">[root@master1 ~]# kubectl create clusterrolebinding dashboard-admin --clusterrole&#x3D;cluster-admin --serviceaccount&#x3D;kube-system:dashboard-admin</span><br><span class="line">[root@master1 ~]# kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk &#39;&#x2F;dashboard-admin&#x2F;&#123;print $1&#125;&#39;)</span><br></pre></td></tr></table></figure>

<p>再使用输出的<code>token</code>登陆<code>dashboard</code>即可。</p>
<p><img src="http://iubest.gitee.io/pic/640-1601003120793.webp" alt="img"></p>
<p><img src="http://iubest.gitee.io/pic/640-1601003120792.webp" alt="img"></p>
<h3 id="报错"><a href="#报错" class="headerlink" title="报错"></a>报错</h3><p>（1）其他master节点无法加入集群</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[check-etcd] Checking that the etcd cluster is healthy</span><br><span class="line">error execution phase check-etcd: error syncing endpoints with etc: context deadline exceeded</span><br><span class="line">To see the stack trace of this error execute with --v&#x3D;5 or higher</span><br></pre></td></tr></table></figure>

<p>查看集群的高可用配置是否有问题，比如keepalived的配置中，主备，优先级是否都配置好了。</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/k8s/kubeadm%E9%AB%98%E5%8F%AF%E7%94%A8master%E8%8A%82%E7%82%B9%E4%B8%89%E4%B8%BB%E4%B8%A4%E4%BB%8E/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/k8s/" rel="tag">k8s</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/interview/IT%E8%BF%90%E7%BB%B4%E9%9D%A2%E8%AF%95%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93-%E8%BF%90%E7%BB%B4%E5%B7%A5%E5%85%B7%E3%80%81%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8(Ceph%E3%80%81Docker%E3%80%81Apache%E3%80%81Nginx%E7%AD%89)/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            IT运维面试问题总结-运维工具、开源应用(Ceph、Docker、Apache、Nginx等).md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/mysql/%E4%B8%87%E5%AD%97%E6%80%BB%E7%BB%93%EF%BC%9A%E5%AD%A6%E4%B9%A0MySQL%E4%BC%98%E5%8C%96%E5%8E%9F%E7%90%86%EF%BC%8C%E8%BF%99%E4%B8%80%E7%AF%87%E5%B0%B1%E5%A4%9F%E4%BA%86/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">万字总结：学习MySQL优化原理，这一篇就够了.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>